<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<style>
.table-bordered,.table-bordered>thead>tr>th, .table-bordered>tbody>tr>th, .table-bordered>tfoot>tr>th, .table-bordered>thead>tr>td, .table-bordered>tbody>tr>td, .table-bordered>tfoot>tr>td{
    border: 1px solid #cdcdcd;
}
</style>
<div class="row">
    <div class="col-sm-12">
        <div class="box">
        <div class="box-header with-border">
       
        <h2 class="page-header">
        <div class="pull-right">  <a href="/backend/adword/account/bind" class="btn btn-sm btn-info btn-flat" >绑定账号</a></div>
          <?php echo form_open(current_url(), array('class' => 'form-inline', 'method'=>'get','id' => 'f')); ?>
                <div class="form-group filtercol" id="filtertype1" style="margin-right:15px;">
                    <?php echo tpl_form_field_daterange("timelimit",$timerange,false);?>
                </div>
                <div class="form-group">
                    <div class="btn-group">
                        <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => '查询')); ?>
                    </div>
                </div>
            <?php echo form_close();?>
           
        </h2>
        
        </div>
        <div class="box-body">

        <table id="example2" class="table table-bordered table-hover dataTable" role="grid" aria-describedby="example2_info">
                <thead>
                <tr role="row">
                    <th tabindex="0" aria-controls="example2" rowspan="1" colspan="1" >账号</th>
                    <th class="sorting" tabindex="1" aria-controls="example2" rowspan="1" colspan="1" aria-sort="ascending">点击次数</th>
                    <th class="sorting" tabindex="2" aria-controls="example2" rowspan="1" colspan="1" >展示次数</th>
                    <th class="sorting" tabindex="3" aria-controls="example2" rowspan="1" colspan="1" >点击率</th>
                    <th class="sorting" tabindex="4" aria-controls="example2" rowspan="1" colspan="1" >CPC</th>
                    <th class="sorting" tabindex="5" aria-controls="example2" rowspan="1" colspan="1" >费用</th>
                    <th class="sorting" tabindex="6" aria-controls="example2" rowspan="1" colspan="1" >转化次数</th>
                    <th class="sorting" tabindex="7" aria-controls="example2" rowspan="1" colspan="1" >转化率</th>
                    <th class="sorting" tabindex="8" aria-controls="example2" rowspan="1" colspan="1" >每次转化费用</th>
                    <th class="sorting" tabindex="9" aria-controls="example2" rowspan="1" colspan="1" >转化价值/费用</th>
                </tr>
                </thead>
                <tbody>
                
                <?php
                 $sum=[
                    "totalclick"=>0,
                    "totalimpressions"=>0,
                    "totalcost"=>0,
                    "totalconversions"=>0,
                    "conversionvalue"=>0.00,
                    "ctr"=>0.00,
                    "conversionrate"=>0.00,
                    "averagecpc"=>0.00,
                    "costperconversion"=>0.00,
                    "convucost"=>0.00
                ];

                if($result):
                $rowcount=0;
               
                foreach($result as $item):
                    if($map[$item["acccountid"]]["currency"]=="CNY")
                        $item["cost"]=sprintf("%.2f",$item["cost"]/7);

                    
                    $sum["totalcost"]+=$item["cost"];
                
                    $item["ctr"]=($item["impressions"]==0?0.00:sprintf("%.2f",$item["click"]/$item["impressions"]*100));
                    $item["conversionrate"]=($item["click"]==0?0.00:sprintf("%.2f",$item["conversions"]/$item["click"]*100));
                    $item["averagecpc"]=($item["click"]==0?0.00:sprintf("%.2f",$item["cost"]/$item["click"]));
                    $item["costperconversion"]=($item["conversions"]==0?"0.00":sprintf("%.2f",$item["cost"]/$item["conversions"]));
                    $item["convucost"]=($item["cost"]==0?0.00:sprintf("%.2f",$item["conversionvalue"]/$item["cost"]));
                    
                    $sum["totalclick"]+=$item["click"];
                    $sum["totalimpressions"]+=$item["impressions"];                    
                    $sum["conversionvalue"]+=$item["conversionvalue"];
                    $sum["totalconversions"]+=$item["conversions"];
                ?>
                <tr role="row" class="<?php if($rowcount%2==0) echo "odd";else echo "even";?>">
                  <td class=""><div><a href="/backend/adword/account/detail/<?php echo $item["acccountid"];?>" style="color:#1a73e8" target="_blank"><?php echo $map[$item["acccountid"]]["accountdesc"];?></div>
                  <div style="color: rgba(0,0,0,.54);"><?php echo $map[$item["acccountid"]]["account"];?></div></td>
                  <td class=""><?php echo $item["click"];?></td>
                  <td class=""><?php echo $item["impressions"];?></td>
                  <td class=""><?php echo $item["ctr"];?>%</td>
                  <td class="">$<?php echo $item["averagecpc"];?></td>
                  <td class="">$<?php echo $item["cost"];?></td>
                  <td class=""><?php echo $item["conversions"];?></td>
                  <td class=""><?php echo $item["conversionrate"];?>%</td>
                  <td class="">$<?php echo $item["costperconversion"];?></td>
                  <td class=""><?php echo $item["convucost"];?></td>
                </tr>
                <?php
                $rowcount++;
                endforeach;

                $sum["ctr"]=sprintf("%.2f",$sum["totalclick"]/$sum["totalimpressions"]*100);
                if($sum["totalclick"]>0) $sum["conversionrate"]=sprintf("%.2f",$sum["totalconversions"]/$sum["totalclick"]*100);
                if($sum["totalclick"]>0) $sum["averagecpc"]=sprintf("%.2f",$sum["totalcost"]/$sum["totalclick"]);
                if($sum["totalconversions"]>0) $sum["costperconversion"]=sprintf("%.2f",$sum["totalcost"]/$sum["totalconversions"]);
                if($sum["totalcost"]>0) $sum["convucost"]=sprintf("%.2f",$sum["conversionvalue"]/$sum["totalcost"]);

                endif;
                ?>
                </tbody>
                <tfoot>
                <tr><th rowspan="1" colspan="1">总计</th>
                <th rowspan="1" colspan="1"><?php echo $sum["totalclick"];?></th>
                <th rowspan="1" colspan="1"><?php echo $sum["totalimpressions"];?></th>
                <th rowspan="1" colspan="1"><?php echo $sum["ctr"];?>%</th>
                <th rowspan="1" colspan="1">$<?php echo $sum["averagecpc"];?></th>
                <th rowspan="1" colspan="1">$<?php echo $sum["totalcost"];?></th>
                <th rowspan="1" colspan="1"><?php echo sprintf("%.2f",$sum["totalconversions"]);?></th>
                <th rowspan="1" colspan="1"><?php echo $sum["conversionrate"];?>%</th>
                <th rowspan="1" colspan="1">$<?php echo $sum["costperconversion"];?></th>
                <th rowspan="1" colspan="1"><?php echo $sum["convucost"];?></th>
                </tr>
                </tfoot>
              </table>

        </div>
    </div>
</div>
<link rel="stylesheet" href="/assets/backend/jquery.dataTables.min.css">
<script type="text/javascript" src="/assets/backend/jquery.dataTables.min.js"></script>
<script>
  $(function () {
    $('#example2').DataTable({
      'paging'      : false,
      'lengthChange': false,
      'searching'   : false,
      'ordering'    : true,
      'info'        : false,
      'autoWidth'   : true,
      "columns": [
            { "orderable": false },
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
        ],

    });
    $("thead th:eq(5)").click().click();
  })
</script>
